package org.liziyi.pss.gui;

import org.liziyi.pss.common.enums.CpuStatusEnum;
import org.liziyi.pss.config.Config;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liziyi
 */
public class CpuGUI {
    public JPanel baseJPanel;
    public JTable cpuJTable;
    public List<JProgressBar> coreJprogressBarList;
    public void createUIComponents() {
        cpuJTable=new JTable(new MyTableModel(Config.cpuCore));
        cpuJTable.setDefaultRenderer(Object.class, new MyTableCellRender());
        // 设置列宽度比例
        cpuJTable.getColumnModel().getColumn(2).setPreferredWidth((int)(cpuJTable.getPreferredSize().width*2));
        cpuJTable.getColumnModel().getColumn(1).setPreferredWidth((int)(cpuJTable.getPreferredSize().width*0.4));
        // 设置行高
        cpuJTable.setRowHeight((int)(cpuJTable.getPreferredSize().height*0.6));
    }
    public class MyTableModel extends AbstractTableModel{
        private Object[][] cells;
        public MyTableModel(int row){
            coreJprogressBarList=new ArrayList<>();
            cells=new Object[row][3];
            for(int i=0;i<row;i++){
                JLabel label0=new JLabel("CPU"+i);
                JLabel label1=new JLabel(CpuStatusEnum.FREE.getDesc());
                label0.setHorizontalAlignment(SwingConstants.CENTER);
                label1.setHorizontalAlignment(SwingConstants.CENTER);
                cells[i][0]=label0;
                cells[i][1]=label1;
                JProgressBar progressBar=new JProgressBar();
                progressBar.setMaximum(100);
                progressBar.setStringPainted(true);
                progressBar.setForeground(Color.green);
                cells[i][2]=progressBar;
                coreJprogressBarList.add(progressBar);
            }
        }

        @Override
        public int getRowCount() {
            return cells.length;
        }

        @Override
        public int getColumnCount() {
            return cells[0].length;
        }
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return cells[rowIndex][columnIndex];
        }

        @Override
        public String getColumnName(int column) {
            return switch (column) {
                case 0 -> "核心";
                case 1 -> "PID";
                case 2 -> "进度";
                default -> null;
            };
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if(columnIndex<2){
                ((JLabel)cells[rowIndex][columnIndex]).setText(aValue.toString());
            }else{
                cells[rowIndex][columnIndex]=aValue;
            }
        }
    }

    public class MyTableCellRender extends JPanel implements TableCellRenderer{
        MyTableCellRender(){
            super(new BorderLayout());
        }
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            this.removeAll();
            if(column<2){
                this.add((JLabel)value,BorderLayout.CENTER);
            } else{
                this.add((JProgressBar)value,BorderLayout.CENTER);
            }
            return this;
        }
    }
}
